package com.yc.register.web;

import com.yc.register.bean.Result;
import com.yc.register.biz.BizException;
import com.yc.register.biz.RegisterBiz;
import com.yc.register.dao.RegisterDao;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class RegisterAction {

    @Resource
    RegisterDao registerDao;

    @Resource
    RegisterBiz registerBiz;

    @GetMapping("show")
    public String show(){
        return registerDao.toString();
    }

    @RequestMapping("register")
    public Result register(String account, String username, String password, String address, String phone, String email, HttpSession session){
        try {
            int register = registerBiz.register(account,username,password,address,phone,email);
            session.setAttribute("registerUser",register);
            return new Result(1,"注册成功!",null);
        } catch (BizException e) {
            e.printStackTrace();
            return new Result(0,e.getMessage(),null);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(0,"业务繁忙,请稍后再试!",null);
        }
    }

    @RequestMapping("editPerson")
    public Object editPerson(String username, String gender, String age, String address, String phone, String email,
                             String school, String education, String profession, String job, String pcode,String account, String password,
                              HttpSession session){
        try {
            Object editperson = registerBiz.editPerson(username, gender, age, address, phone, email, school, education, profession, job, pcode,account, password);
            session.setAttribute("editperson",editperson);
            return new Result(1,"修改个人信息成功!",null);
        } catch (BizException e) {
            e.printStackTrace();
            return new Result(0,e.getMessage(),null);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(0,"业务繁忙,请稍后再试!",null);
        }
    }

    @RequestMapping("editCount")
    public Object editCount(String newpwd ,String email,String oldpwd,HttpSession session ){
        try {
            Object editcount = registerBiz.editCount(newpwd,email,oldpwd);
            session.setAttribute("editcount",editcount);
            return new Result(1,"修改密码成功!",null);
        } catch (BizException e) {
            e.printStackTrace();
            return new Result(0,e.getMessage(),null);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(0,"业务繁忙,请稍后再试!",null);
        }
    }

}
